<?php
namespace app\index\controller;
use think\Controller;
use think\Db;
use think\Session;

class Friend extends Controller
{
    public function goodFriend(){
    	$user_id = Session::get('id');//当前登录用户的id
    	if($user_id){
	//  	$friend_info = Db::table("tp_user")->where(array("friend_id"=>$user_id))->select();
			//好友列表id
			/*****好友列表start******/
			$friend_id = Db::table("tp_user")->alias("u")
						->field("f.friend_id,f.user_id")
						->join("tp_friend f","u.id = f.user_id","left")
						->where(array("f.user_id"=>$user_id))
						->whereOr(array("f.friend_id"=>$user_id))
						->select();
			$bool = (bool) $friend_id;
			
			$id_info = array();
			if($bool){
				foreach($friend_id as $k => $v){
					if($v['friend_id'] == $user_id){
						$id_info[] = $v['user_id'];
					}else{
						$id_info[] = $v['friend_id'];
					}
				}
				foreach($id_info as $key => $val){
					$friend_info[$key] = Db::table("tp_user")->where(array("id"=>$id_info[$key]))->find();
				}
	    		$this->view->assign("friend",$friend_info);
				
			}
			/*****好友列表end******/

			/*【好友页面聊天列表】 	start*/
			$arr1 = Db::table("tp_chat")->distinct(true)->field('user_id as id')->where(array("friend_id"=>$user_id))->select();
			$arr2 = Db::table("tp_chat")->distinct(true)->field('friend_id as id')->where(array("user_id"=>$user_id))->select();
			//合并多个数组
			$fellow_id = array_merge($arr1,$arr2);
			foreach($fellow_id as $k => $v){
				$arr[$k] = $v['id'];
			}
			//去除数组重复的项
			$people = array_unique($arr);
			$bools = (bool) $people;
			
			
			
			if($bools){
				foreach($people as $k => $v){
					$field2 = "u.img,u.name";
					$where2 = "(c.friend_id <> '".$user_id."' AND c.user_id = '".$v."') OR (c.friend_id = '".$v."' AND c.user_id = '".$user_id."')";
					$info = Db::table('tp_chat')->alias('c')
							->join('tp_user u','c.friend_id = u.id','left')
							->field($field2)
							->where($where2)
							->order('c.create_time desc')
							->find();
							
					/*将两个好友间的聊天的最后一条消息的内容和时间查询出来放入$info    start*/
					$c_where = "(friend_id = '".$user_id."' AND user_id = '".$v."') OR (friend_id = '".$v."' AND user_id = '".$user_id."')";	
					$content = Db::table('tp_chat')->field('content,create_time')->where($c_where)->order('create_time desc')->find();
					$info['content'] = $content['content'];
					$info['create_time'] = date("H:i",$content['create_time']);
					/*将两个好友间的聊天的最后一条消息的内容和时间查询出来放入$info    end*/
					
					$info['friend_id'] = $v;
					$chat[$k] = $info;
				}
				
			}
			/*【好友页面聊天列表】 	end*/
			
	    	$this->view->assign("chat",$chat);
	    	$this->view->assign("bool",$bool);
	
	    	return $this->view->fetch();
    	}else{
    		echo "<script>alert('您还未登录，请先登录！');window.location.href='/index/login/login_deng'</script>";
    	}
    }
    public function onlineChat(){
    	$user_id = Session::get('id');
    	$friend_id = $_GET['friend_id'];
    	$friend_name = Db::table("tp_user")->where(array("id"=>$friend_id))->find();
    	
    	$field = "c.*,u.img,u.name";
    	$data['c.friend_id'] = $user_id;
    	$data['c.user_id'] = $friend_id;
    	
    	$whereOr = "c.friend_id = $friend_id and c.user_id = $user_id";
    	$chat_info = Db::table('tp_chat')->alias('c')
    				 ->join('tp_user u','c.user_id = u.id','left')
    				 ->field($field)
    				 ->where($data)
    				 ->whereOr($whereOr)
    				 ->order('c.create_time asc')
    				 ->select();
    				 
    	$this->view->assign("user_id",$user_id);
    	$this->view->assign("friend_name",$friend_name);
    	$this->view->assign("friend_id",$friend_id);
    	$this->view->assign("chat_info",$chat_info);
    	return $this->view->fetch();
    }
    public function search(){
    	$user_id = Session::get('id');
    	
    	$like = $_GET['like'];
		$map['u.name']  = ['like',"%$like%"];
//		$map['u.id']  = ['=',$user_id];
    	$map['f.friend_id']  = ['=',$user_id];
    	$info = Db::table("tp_user")->alias('u')
    			->join('tp_chat c','u.id = c.user_id','left')
    			->join('tp_friend f','u.id = f.user_id')
    			->where("`f`.`friend_id` = $user_id and (`u`.`name` LIKE '%$like%'  OR `c`.`content` LIKE '%$like%')")
    			->distinct(true)->field('u.*')
    			->select();
//  			var_dump(Db::table("tp_user"));exit;
    	$this->view->assign("info",$info);
    	return $this->view->fetch();
    }
    public function add_chat(){
    	$id = Session::get('id');
    	$data['user_id'] = $id;
    	$data['content'] = $_POST['content'];
    	$data['create_time'] = time();
    	$data['friend_id'] = $_POST['friend_id'];
    	
    	$res = Db::table("tp_chat")->insert($data);
    	if($res){
    		$field = "c.content,u.img,c.user_id";
    		$new = Db::table('tp_chat')->alias('c')
    			   ->join('tp_user u','c.user_id = u.id','left')
    			   ->field($field)
    			   ->order('c.id desc')
    			   ->find();
    		$info = [
				'code' => 1,
				'data' => $new
			];
			return json_encode($info);
    	}
    }
	/*
	 * 群聊界面
	 */
	public function groupsad(){
		return $this->view->fetch();
	}
	/*
	 * 删除好友
	 */
	public function deletefriend(){
		$fid = $_POST['friend'];
		$id = Session::get('id');
		
		$where = "(friend_id = $fid and user_id = $id) or (friend_id = $id and user_id = $fid)";
		$res= Db::table("tp_friend")->where($where)->delete();
		if($res){
			echo "1";exit;
		}else{
			echo "-1";exit;
		}
	}
}
